package com.yd.app.service.common;

import com.yd.app.mapper.LoggerMapper;
import com.yd.domain.commom.BusinessException;
import com.yd.domain.commom.Exceptions;
import com.yd.domain.commom.PageModel;
import com.yd.domain.sys.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zy
 * @create 2018-03-12 14:13
 * @desc 日志service
 **/
@Service
public class LoggerService extends BaseService{
    @Autowired
    LoggerMapper loggerMapper;

    /**
     * 添加日志
     * @param logger
     */
    public void addLogger(Logger logger, HttpServletRequest request) {
        if(logger.getTitle().equals("")){
            new BusinessException(Exceptions.X9999, "LoggerController","标题不能为空！！！" );
        }else if(logger.getStatus().equals("")){
            new BusinessException(Exceptions.X9999, "LoggerController","操作状态不能为空！！！" );
        }else if(logger.getLog_type().equals("")){
            new BusinessException(Exceptions.X9999, "LoggerController","操作类型不能为空！！！" );
        }else if(logger.getOperUser().equals("")){
            new BusinessException(Exceptions.X9999, "LoggerController","操作人不能为空！！！" );
        }
        //获取当前ip
        logger.setIpAddress(request.getRemoteHost());
        loggerMapper.addLogger(logger);
    }

    /**
     * 查询日志
     * @param searchParam
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public PageModel getLoggerList(Logger searchParam,int pageIndex, int pageSize) {
        PageModel pm = new PageModel();
        List<Logger> loggerList=loggerMapper.getLoggerList(searchParam, getStartRow(pageIndex, pageSize), pageSize);
        pm.setList(loggerList);
        int count = 0;
        if (pageIndex == 1) {
        //查询总条数
        try {
            count = loggerMapper.getLoggerListCount(searchParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
        pm.setCount(count);
        }
        return pm;
    }

    /**
     * 根据id获取日志信息
     * @param id
     * @return
     */
    public Logger getLogById(String id) {
        Logger logger=loggerMapper.getLogById(id);
        return  logger;
    }
}
